package com.wanmait.zaisousuo.filter;

import com.wanmait.zaisousuo.pojo.Admin;
import com.wanmait.zaisousuo.pojo.Userinfo;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "AdminLoginFilter",value ={"/backstage/*"})
public class AdminLoginFilter implements Filter
{
    public void destroy() {}
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;

        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        if(admin==null)
        {
            String path = request.getRequestURI();
            //请求的/backstage/manager/login 放行
            if(path.contains("/backstage/manager/login"))
            {
                chain.doFilter(request,response);
            }
            else
            {
                response.sendRedirect(request.getContextPath()+"/backstage/manager/login");
            }
        }
        else {
            chain.doFilter(req, resp);
        }
    }

    public void init(FilterConfig config) throws ServletException {}
}
